package com.llap.exp190929.service;

import com.llap.exp190929.entity.Invitation;
import com.llap.exp190929.repository.InvitationRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
@Slf4j
public class InvitationService {
    private InvitationRepository invitationRepository;

    public Invitation findInvitation(int id) {
        return invitationRepository.findById(id).orElse(null);
    }

    public Page<Invitation> findInvitations(int page, int pageSize) {
        return invitationRepository.findAll(PageRequest.of(page - 1, pageSize));
    }

    @Transactional
    public Invitation addInvitation(Invitation invitation) {
        if (invitation.getId() != null) return null;
        return invitationRepository.save(invitation);
    }

    @Transactional
    public Invitation updateInvitation(Invitation invitation) {
        if (invitation.getId() == null) return null;
        return invitationRepository.save(invitation);
    }

    @Transactional
    public void deleteInvitation(int id) {
        invitationRepository.deleteById(id);
    }
}
